<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Admin\Traits\AdminTrait;
use App\Models\Admin\AdminOperateLogs;
use App\Models\Admin\AdminOperateModule;
use App\Models\Admin\AdminOperateType;
use App\Models\Admin\AdminUser;

class OperateLogController extends AdminController
{
    use AdminTrait;

    protected $model;

    public function __construct(AdminOperateLogs $model)
    {
        $this->model = $model;
    }

    protected function beforeIndex()
    {
        $operate_type = AdminOperateType::pluck('name','id');
        $operate_module = AdminOperateModule::where('pid',0)->where('status',1)->pluck('name','id');
        $user = AdminUser::pluck('name','id');
        return ['operate_type'=>$operate_type,'operate_module'=>$operate_module,'user'=>$user];
    }

    protected function search($request)
    {
        $start_time = $request->query('start_time');
        $end_time = $request->query('end_time');

        $select = $this->model->with(['module','operate','type','admin']);

        ($user_id = $request->query('user_id')) && $select = $select->where('user_id',$user_id);
        ($module = $request->query('module')) && $select = $select->where('module',$module);
        ($type = $request->query('type')) && $select = $select->where('type',$type);
        ($main = $request->query('main')) && $select = $select->where('main','like',"%$main%");
        if ($start_time && $end_time){
            $select = $select->whereBetween('created_at',date_assemble($start_time,$end_time));
        }
        return $select;
    }
}
